package com.itheima.controller;
import com.itheima.dto.LoginDto;
import com.itheima.pojo.Emp;
import com.itheima.service.EmpService;
import com.itheima.util.JwtUtils;
import io.jsonwebtoken.Jwts;
import com.itheima.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;
@RestController
public class LoginController {
    @Autowired
    private EmpService empService;
    @PostMapping("/login")
    public Result login(@RequestBody LoginDto loginDto){
       Emp emp = empService.login(loginDto) ;

       //登陆成功后 创建token
        Map<String,Object> claims=new HashMap<>();
        claims.put("id",emp.getId());
        claims.put("username",emp.getUsername());
        String token= JwtUtils.generateJwt(claims);
        return Result.success(token);
    }
}
